﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.ConfigCmd
{
    public partial class FormDoorCtrl : Form
    {
        private string id;
        private string devId;
        private string monitorId;
        private string monitorId_Name;
        bool frontDoor;

        public FormDoorCtrl(string deviceId, string ID, string MonitorDev_Id, string monitorDevId_Name, bool doorType)
        {
            InitializeComponent();

            devId = deviceId;
            monitorId = MonitorDev_Id;
            monitorId_Name = monitorDevId_Name;
            id = ID;
            frontDoor = doorType;
        }

        private void updateTb_door()
        {
            try
            {
                MONITOR_DBDataContext db = Monitor_DB.instance.db;

                if (db == null)
                {
                    MessageBox.Show("Data Base is NULL!");
                }

                int deviceId = Convert.ToInt32(id);

                //string anotherDoorType = "";

                // 前后门
                IEnumerable<tb_door> front_door =
                    from doo in db.tb_door
                    where doo.MonitorDevice_Id == Convert.ToInt32(monitorId)
                    select doo;

                tb_door door_frontItem = null;
                foreach (tb_door item in front_door)
                {
                    door_frontItem = item;

                    if (door_frontItem.Door_Type == "前")
                    {
                        if (this.checkBox1.Checked == true)
                        {
                            door_frontItem.Status = true;
                        }

                        //anotherDoorType = "后";
                    }
                    else
                    {
                        if (this.checkBox2.Checked == true)
                        {
                            door_frontItem.Status = true;
                        }

                        //anotherDoorType = "前";
                    }

                    break;
                }

                //// 前后门
                //IEnumerable<tb_door> back_door =
                //    from doo in db.tb_door
                //    where doo.MonitorDevice_Id == Convert.ToInt32(monitorId) && doo.Door_Type == anotherDoorType
                //    select doo;

                //tb_door door_backItem = null;
                //foreach (tb_door item in back_door)
                //{
                //    door_backItem = item;

                //    if (this.checkBox2.Checked == true)
                //    {
                //        door_backItem.Status = true;
                //    }

                //    break;
                //}

                db.SubmitChanges();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Monitor_DB.instance.Refresh_DB();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string doorOpen = "";
            if (this.checkBox1.Checked)
            {
                doorOpen = "前门; ";
            }

            if (this.checkBox2.Checked)
            {
                doorOpen += "后门;";
            }

            if (ConfigCmd.configCommand.instance.RequestDoorCtrl(devId, monitorId, this.checkBox1.Checked, this.checkBox2.Checked))
            {
                UserLog.adduseroperlog(User.UserID, User.Username, "开门", "成功", monitorId_Name, devId, doorOpen);

                MessageBox.Show("已进入发送队列！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                // 更新tb_door表
                updateTb_door();
             
                this.Dispose();
            }
            else
            {
                UserLog.adduseroperlog(User.UserID, User.Username, "开门", "失败", monitorId_Name, devId, doorOpen);
                MessageBox.Show("发送失败!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
          
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void FormDoorCtrl_Load(object sender, EventArgs e)
        {
            if (frontDoor)
            {
                this.checkBox1.Checked = true;
            }
            else
            {
                this.checkBox2.Checked = true;
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                this.checkBox2.Checked = false;
            }
            else
            {
                this.checkBox2.Checked = true;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox2.Checked)
            {
                this.checkBox1.Checked = false;
            }
            else
            {
                this.checkBox1.Checked = true;
            }
        }

    }
}
